// JavaScript Document
var rowid = "row_id";            //tr id
var zoneid = "div_id";		// zone name td id
var stateid = "state_id";		// disable enable span id
var cancel = "";
function Inint_AJAX() 
{
	try 
	{ 
		return new ActiveXObject("Msxml2.XMLHTTP");  
	} 
	catch(e) 
	{} //IE
	try 
	{ 
		return new ActiveXObject("Microsoft.XMLHTTP"); 
	} 
	catch(e) {} //IE
	try 
	{ 
		return new XMLHttpRequest(); 
	} catch(e) {} //Native Javascript
	alert("XMLHttpRequest not supported");
	return null;
};


function edit_div(id) 
{ 

	$(".div_row").hide();
	$("#"+rowid+"_"+id).show();
	cancel = document.getElementById(rowid+"_" + id).innerHTML;

	var editmode = document.getElementById(rowid+"_" + id);
	editmode.innerHTML = "Loading.....";
	var http = Inint_AJAX();
	http.onreadystatechange = function () 
	{ 	
		if(http.readyState == 4 && http.status == 200) {
		editmode.innerHTML = http.responseText;
		}
	};
	http.open("post", "call_ajax_division_handler.php?edit_id=" + id,true); 
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	http.send(null); 
	
}

function default_row(id){
	$(".div_row").show();
	 document.getElementById(rowid+"_" + id).innerHTML = cancel;
}


function save_changes(id,div_name,circle_id,zone_id){
var d_name  = document.getElementById(div_name);
var c_id  = document.getElementById(circle_id);
var z_id  = document.getElementById(zone_id);
var row_change = document.getElementById(rowid+"_"+id);

	if(z_id.value==0){
	$("#circle_msg").html("<font color='red'>zone is not selected</font>");
	z_id.focus();
	return;
	}
	if(c_id.value==0){
	$("#circle_msg").html("<font color='red'>circle is not selected</font>");
	c_id.focus();
	return;
	}
	if(d_name.value==0){
	$("#circle_msg").html("<font color='red'>Division name is entered</font>");
	d_name.focus();
	return;
	}



	
	
	row_change.innerHTML = "<td colspan='4' align='center'><b>Saving.....</b></td>";
	
	var http = Inint_AJAX();
	http.onreadystatechange = function () 
	{ 	
		if(http.readyState == 4 && http.status == 200) {
		//alert(http.responseText);
		row_change.innerHTML = http.responseText;
		$(".div_row").show();
		}
	};
	http.open("post", "call_ajax_division_handler.php?new_div="+escape(d_name.value)+"&&new_circle="+ c_id.value + "&&new_zone="+ z_id.value +"&&id="+id,true); 
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	http.send(null); 
}





function add_division(zone,circle,value,msg) {
var div_name = document.getElementById(value);
var zone_id = document.getElementById(zone);
var circle_id = document.getElementById(circle);


	if(zone_id.value == 0) {
	$("#"+msg).html("<font color='red'>zone is required</font>");
	zone_id.focus();
	return false;
	}
	if(circle_id.value ==0){
	$("#"+msg).html("<font color='red'>Circle is required</font>");
	circle_id.focus();
	return false;
	}
	if(div_name.value == "") {
		$("#"+msg).html("<font color='red'>Division name is required</font>");
		div_name.focus();
		return false;
	}
	else {
		$("#circle_msg").html("Saving.....");
		var http = Inint_AJAX();
		http.onreadystatechange = function () 
		{ 	
			if(http.readyState == 4 && http.status == 200) {
			
			$("#table1 > #content_zone:First").prepend(http.responseText);
			$("#circle_msg").html("Division is added successfully");
			$("#add_form").toggle();
			}
		};
		http.open("post", "call_ajax_division_handler.php?add_div="+escape(div_name.value)+ "&&circle_id="+circle_id.value+ "&&zone_id=" + zone_id.value,true); 
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
		http.send(null); 
		
	return false;
	}


//	return false;
}

function load_circle(value,select_id) {
	var circle = document.getElementById(select_id);
	circle.innerHTML = "<option value='0'>Loading..</option>";

		var http = Inint_AJAX();
		http.onreadystatechange = function () 
		{ 	
			if(http.readyState == 4 && http.status == 200) {
					
			circle.innerHTML = http.responseText;
			}
		};
		http.open("post", "call_ajax_division_handler.php?load_circle="+value,true); 
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
		http.send(null); 

}

function status_handler(value,id) {
	var changetag = document.getElementById(stateid+"_"+id);
	changetag.innerHTML ="<font style='font-size: 8.5pt' face='Verdana' >Loading....</font>";
	var http = Inint_AJAX();
	http.onreadystatechange = function () 
	{ 	
		if(http.readyState == 4 && http.status == 200) {
		changetag.innerHTML = http.responseText;
		}
	};
	http.open("post", "call_ajax_division_handler.php?status=" + value + "&&id=" + id,true); 
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	http.send(null); 
	

}
function search_id(value){
	if(value ==""){
	$(".div_row").show();
	$("#circle_msg").html("");
	return ;
	}
	
$(".div_row").hide();
$("#circle_msg").html("Loading....");
var http = Inint_AJAX();
	http.onreadystatechange = function () 
	{ 	
		if(http.readyState == 4 && http.status == 200) {
			
			$("#circle_msg").html("");
		var id_str = JSON.parse(http.responseText);
			if(id_str.length ==0){
			$("#circle_msg").html("No Record Found");
			} 
			for(i=0;i< id_str.length;i++) {
				$("#row_id_"+id_str[i].div_id).show();
			} 
		}
	};
	http.open("post", "call_ajax_division_handler.php?search=" + value,true); 
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	http.send(null); 

}

$(document).ready(function(){
						  
						  $("#circle_msg").html("Loading...");
						  $("#content_zone").load("call_ajax_division_handler.php?load=&&start=1",function(){
							$("#circle_msg").html("");	
							});
						  
});


function ajax_content(value,start){
	
	$("#circle_msg").html("Loading");
	
	document.getElementById("content_zone").style.opacity=0.5;
	$(".div_row").hide();
	
	
	//alert(start);
	
	var http = Inint_AJAX();
	http.onreadystatechange = function () 
	{ 	
		if(http.readyState == 4 && http.status == 200) {
			document.getElementById("content_zone").style.opacity=1;
			$("#content_zone").html(http.responseText);
			$("#circle_msg").html("");
			
		
		}
	};
	http.open("post", "call_ajax_division_handler.php?load=" + value+ "&&start="+start,true); 
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	http.send(null); 

	
}

